﻿
@{
    Layout = "~/Views/Shared/_LayuiLayout.cshtml";
}
<form class="layui-form mb-50">
    <blockquote class="layui-elem-quote">
        <div>
            <input type="checkbox" title="子项全选" lay-skin="primary" id="cbChildren" checked="checked"/>
            @*<input type="checkbox" title="即时更新" lay-skin="primary" id="cbImmediately" />*@
        </div>
    </blockquote>

    <table class="layui-table" id="tbList">
        <colgroup>
            <col width="300" />
            <col width="100" />
        </colgroup>
        <thead>
            <tr>
                <th>页面权限</th>
                <th>是否授权</th>
                <th>功能权限</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model)
            {
                <tr>
                    <td>@item.MenuName</td>
                    <td>
                        <input type="checkbox" lay-skin="switch" value="@item.Id" id="@item.Id"
                               lay-text="是|否" lay-filter="menu" @(item.IsEnable ? "checked=\"checked\"" : "") />
                    </td>
                    <td>
                        @foreach (var btn in item.ButtonList)
                        {
                            <input type="checkbox" title="@btn.ButtonName" value="@btn.Id" id="@btn.Id"
                                   lay-filter="button" @(btn.IsEnable ? "checked=\"checked\"" : "") />
                        }
                    </td>
                </tr>
            }
        </tbody>
    </table>

    <div class="layui-form-item" align="center">
        <button type="button" class="layui-btn" lay-submit="" lay-filter="Save">保存</button>
        <button type="button" class="layui-btn layui-btn-primary" id="btnCancel">取消</button>
    </div>
</form>

@section scripts{
<script>
    var $, layer;
    layui.use(['form'], function () {
        var form = layui.form;
        $ = layui.jquery;
        layer = parent.layer === undefined ? layui.layer : parent.layer;

        //监听关闭按钮
        $('#btnCancel').on('click', function () {
            layer_close();
        });

        //监听启用开关
        form.on('switch(menu)', function (data) {
            if($("#cbChildren").prop("checked")){
                @foreach(RolePermissionOutput item in Model)
                {
                    <text>
                if ( '@item.ParentId'== data.value)
                {
                    $("#@item.Id").attr("checked",'true');
                    @foreach(SysButton btn in item.ButtonList)
                    {
                        <text>
                    $("#@btn.Id").attr("checked", 'true');
                    </text>
                    }

                    @foreach (RolePermissionOutput item1 in Model)
                    {
                        <text>
                    if ( '@item1.ParentId'== '@item.Id'){
                        $("#@item1.Id").attr("checked",'true');
                        @foreach(SysButton btn1 in item1.ButtonList)
                        {
                        <text>
                    $("#@btn1.Id").attr("checked", 'true');
                        </text>
                    }
                    }
                </text>
            }

                }
                else if(data.value=="@item.Id"){
                    @foreach(SysButton btn in item.ButtonList)
                    {
                        <text>
                    $("#@btn.Id").attr("checked", 'true');
                    </text>
                }
                }
                </text>
                }
            }
            form.render();
        });

        //
        //摘要：
        //  保存角色权限
        //参数：
        //  menuIds: 已选中菜单ID
        function save(menuIds) {
            var loading = layer.load();
            $.post('@Url.Action("SaveRolePermission", "Authority")'
                , {
                    roleId: @ViewBag.RoleID,
                    menuIds: menuIds
                }
                , function (result) {
                    layer.close(loading);
                    if (result.Success) {
                        layer.msg(result.Message, { icon: 1, time: 1000 });
                    } else {
                        layer.alert(result.Message, { icon: 2 });
                    }
                });
        }

        //监听保存
        form.on('submit(Save)', function (data) {
            var menuIds = '', douhao = '';
            $('#tbList').find('input[type=checkbox]').each(function (index, elem) {
                if ($(elem).is(':checked')){  // 选中的
                    menuIds += douhao + $(elem).val();
                    douhao = ',';
                }
            });
            if (menuIds.length == 0) { // 已选权限为空
                layer.confirm('所选权限为空，确定移除当前角色权限吗？'
                    , function(index)
                    {
                        save(menuIds);
                        layer.close(index);
                    });
                return false;
            } else {
                save(menuIds);
            }
            return false;
        });
    });
</script>
    }


